package com.szss.springbootfirst.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wcx
 * @date 2023/2/10 14:43
 */

@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_DIRECT = "exchange_direct";

    public static final String QUEUE_EMAIL = "queue_email";

    public static final String EMAIL_KEY = "email_key";

    //定义交换机交给spring容器
    @Bean
    public Exchange exchangeDeclare(){
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT).build();
    }

    //声明队列交给spring容器
    @Bean(QUEUE_EMAIL)
    public Queue queueEmailDeclare(){
        return QueueBuilder.durable(QUEUE_EMAIL).build();
    }

    //绑定交换机和队列
    @Bean
    public Binding bindEmailQueueToExchange(@Qualifier(QUEUE_EMAIL) Queue queue, Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EMAIL_KEY).noargs();
    }

}
